//
//  DRPickerView.m
//  DoctorRen
//
//  Created by Macx on 2018/12/17.
//

#import "DRPickerView.h"

@interface DRPickerView()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@property (nonatomic, strong) NSArray *data1;

@property (nonatomic, strong) NSArray *data2;

@property (nonatomic, strong) NSString *string1;

@property (nonatomic, strong) NSString *string2;

@end

@implementation DRPickerView

-(void)awakeFromNib{
    [super awakeFromNib];
    self.data1 = @[];
    self.data2 = @[];
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];//自定义动画
    CATransition *animation = [CATransition animation];
    animation.duration = 0.3;
    animation.type = kCATransitionFade;
    animation.subtype = kCATransitionFromTop;
    [self.layer addAnimation:animation forKey:nil];
    
}

-(void)setPickerData:(NSArray<NSArray *> *)pickerData{
    if (pickerData.count == 2) {
        self.data1 = pickerData[0];
        self.data2 = pickerData[1];
        [self.pickerView reloadAllComponents];
    }
}

-(void)setTitle:(NSString *)title{
    self.titleLabel.text = title;
}

-(void) hiddenAnimation{
    CATransition *animation = [CATransition animation];
    animation.duration = 0.3;
    animation.type = kCATransitionFade;
    animation.subtype = kCATransitionFromBottom;
    [self.layer addAnimation:animation forKey:nil];
    [self removeFromSuperview];
}

- (IBAction)cancelAction:(id)sender {
    [self hiddenAnimation];
}
- (IBAction)confirmAction:(id)sender {
    if (self.confirmBlock) {
        self.confirmBlock(self.string1, self.string2);
    }
    [self hiddenAnimation];
}

-(void)setDefaultRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        self.string1 = self.data1[row];
    }else{
        self.string2 = self.data2[row];
    }
    [self.pickerView selectRow:row inComponent:component animated:NO];
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.data1.count;
    }else{
        return self.data2.count;
    }
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return self.data1[row];
    }else{
        return self.data2[row];
    }
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        self.string1 = self.data1[row];
    }else{
        self.string2 = self.data2[row];
    }
    NSLog(@"row:%@",@(row));
}

@end
